﻿using System;
using System.Text;

using mfRCF.Core;
using System.Net;
using System.IO;

namespace mfRCF.netmf
{
    public class mfHttpRequest : mfIHttpRequest
    {
        #region CTOR

        public mfHttpRequest(HttpListenerRequest request, mfIHttpAuthenticator authenticator) 
        {
            mfRequest = request;
            Authenticator = authenticator;

            if (mfRequest.Credentials != null)
            {
                mfCredential = new mfHttpCredential()
                {
                    UserName = mfRequest.Credentials.UserName
                    ,
                    Password = mfRequest.Credentials.Password
                };

                IsAuthenticated = Authenticator.Authenticate(mfCredential);
            }
        }
        #endregion

        #region Properties

        private HttpListenerRequest mfRequest { get; set; }
        private mfIHttpCredential mfCredential { get; set; }
        #endregion

        #region mfIHttpRequest
        public mfIHttpAuthenticator Authenticator
        {
            get;
            private set;
        }

        public bool IsAuthenticated
        {
            get;
            private set;
        }

        public Stream InputStream
        {
            get
            {
                return mfRequest.InputStream;
            }
        }

        /// <summary>
        /// the relative requested url from client
        /// </summary>
        public string RawUrl
        {
            get
            {
                return mfRequest.RawUrl;
            }
        }

        public string UserHostName
        {
            get
            {
                return mfRequest.UserHostName;
            }
        }

        public string UserHostAddress
        {
            get
            {
                return mfRequest.UserHostAddress;
            }
        }

        public string HttpMethod
        {
            get
            {
                return mfRequest.HttpMethod;
            }
        }

        public mfIHttpCredential Credential
        {
            get { throw new NotImplementedException(); }
        }

        public string ContentType
        {
            get
            {
                return mfRequest.ContentType;
            }
        }

        public void Reset()
        {
            mfRequest.Reset();
        }
        #endregion
    }
}
